package cn.jantd.modules.system.service.impl;

import cn.jantd.modules.system.entity.SysRoleTeamuser;
import cn.jantd.modules.system.entity.SysTeam;
import cn.jantd.modules.system.entity.SysTeamRole;
import cn.jantd.modules.system.mapper.SysRoleTeamuserMapper;
import cn.jantd.modules.system.service.ISysRoleTeamuserService;
import cn.jantd.modules.system.service.ISysTeamRoleService;
import cn.jantd.modules.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @Author xiagf
 * @date 2019-07-04
 */
@Service
public class SysRoleTeamuserServiceImpl extends ServiceImpl<SysRoleTeamuserMapper, SysRoleTeamuser> implements ISysRoleTeamuserService {

    @Autowired
    ISysTeamService teamService;
    @Autowired
    ISysTeamRoleService teamroleService;
    @Autowired
    private SysRoleTeamuserMapper teamuserMapper;

    /**
     * queryUserRole调用的方法
     *
     * @param roleList
     * @param teamroleId
     * @return
     */
    private String searchByTeamroleId(List<SysTeamRole> roleList, String teamroleId) {
        while (true) {
            for (SysTeamRole role : roleList) {
                if (teamroleId.equals(role.getId())) {
                    return role.getTeamRolename();
                }
            }
        }
    }

    /**
     * 查询所有团队对应的角色信息
     */
    @Override
    public Map<String, String> queryTeamRole() {
        List<SysRoleTeamuser> uRoleList = this.list();
        List<SysTeam> teamList = teamService.list();
        List<SysTeamRole> roleList = teamroleService.list();
        Map<String, String> map = new IdentityHashMap<>();
        String teamId = "";
        String teamroleId = "";
        String teamRolename = "";
        if (!uRoleList.isEmpty()) {
            for (SysRoleTeamuser uRole : uRoleList) {
                teamroleId = uRole.getTeamroleId();
                for (SysTeam team : teamList) {
                    teamId = team.getId();
                    if (uRole.getTeamId().equals(teamId)) {
                        teamRolename = this.searchByTeamroleId(roleList, teamroleId);
                        map.put(teamId, teamRolename);
                    }
                }
            }
            return map;
        }
        return map;
    }

    @Override
    public List<SysRoleTeamuser> getByTeamId(String teamId) {
        List list = teamuserMapper.getByTeamId(teamId);
        return list;
    }
}
